/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.models
{
	/**
	 * $Revision: 258 $<br/>
	 * EntityModel manager.
	 * 
	 * @example
	 * There are three classes. CountSample should be an application. This class instantiates following two classes, MyController and MyView.
	 * MyController counts up to 100. MyView outputs 1 to 100 in a console. MyController and MyView are using EntityModels class.
	 * <listing version="3.0">
	 * package
	 * {
	 * 	import controllers.MyController;
	 * 	import flash.display.Sprite;
	 * 	import views.MyView;
	 * 
	 * 	public class CountSample extends Sprite
	 * 	{
	 * 		public function CountSample()
	 * 		{
	 * 			addChild(new MyController());
	 * 			addChild(new MyView());
	 * 		}
	 * 		
	 * 	}
	 * }
	 * </listing>
	 * MyController class
	 * <listing version="3.0">
	 * package controllers
	 * {
	 * 	import com.puny.mvc.controllers.Controller;
	 * 	import com.puny.mvc.models.EntityModels;
	 * 	import com.puny.mvc.models.Model;
	 * 
	 * 	public class MyController extends Controller
	 * 	{
	 * 		private var _model:Model
	 * 		public function MyController()
	 * 		{
	 * 			super();
	 * 		}
	 * 		
	 * 		override protected function setup():void {
	 * 			_model = EntityModels.getInstance("myModel");
	 * 			_model["counter"] = 0;
	 * 		}
	 * 		
	 * 		override public function procedureMainFrame():Boolean {
	 * 			_model["counter"]++
	 * 			return _model["counter"] &lt; 100;
	 * 		}
	 * 		
	 * 	}
	 * }
	 * </listing>
	 * 
	 * MyView class
	 * <listing version="3.0">
	 * package views
	 * {
	 * 	import com.puny.mvc.models.EntityModels;
	 * 	import com.puny.mvc.models.Model;
	 * 	import com.puny.mvc.views.View;
	 * 	import flash.events.Event;
	 * 
	 * 	public class MyView extends View
	 * 	{
	 * 		private var _model:Model;
	 * 		public function MyView()
	 * 		{
	 * 			super();
	 * 		}
	 * 		
	 * 		override protected function setup():void {
	 * 			_model = EntityModels.getInstance("myModel");
	 * 			_model.addEventListener("counter", countHandler);
	 * 		}
	 * 		
	 * 		private function countHandler(event:Event):void {
	 * 			trace(event.currentTarget["counter"]); // Count up 1 to 100
	 * 		}
	 * 		
	 * 		override public function beforeClose():void {
	 * 			//remove event listeners
	 * 			_model.removeEventListener("counter", countHandler);
	 * 		}
	 * 	}
	 * }
	 * </listing>
	 * 
	 * @see com.puny.mvc.models.Model
	 */
	public class EntityModels
	{
		private static var _internalCall:Boolean = true;
		private static var _models:Object = new Object();
		
		public function EntityModels()
		{
			super();
		}

		/**
		 * Get an instance by name. If the instance doesn't exist, it will be created.
		 */
		public static function getInstance(modelName:String):Model {
			if (_models[modelName] == null) {
				_internalCall = true;
				trace("A new model instance, \""+modelName+"\" , was created.");
				_models[modelName] = createInstance();
				_internalCall = false;
			}
			return _models[modelName];
		}
		
		private static function createInstance():Model {
			return new Model();
		}
		
		/**
		 * Dump all models under this class.
		 */
		public static function dump():void {
			for(var label:* in _models) {
				trace("Model:", label);
				trace(_models[label].toString());
			}
		}
		
		public static function cleanup():void {
			var model:Model;
			for(var label:String in _models) {
				model = _models[label];
				model.cleanUp();
				_models[label] = null;
			}
		}
		
	}
}